<?php

namespace common\Http;

use Log;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;
use Common\Manager\CacheManager;


/**
 * 自定义接口请求客户端
 */
class IHttpClient
{
    private $HttpStatusCode;
    private $config=array(
        'timeout'=>5.14,
        'http_errors'=>false,//false 不显示异常 true 显示异常
        'allow_redirects'=>true,//false 禁止重定向 true 允许重定向
        'headers' => [
            'Accept'     => 'application/json',
            'User-Agent'    => 'ishangzu.com/1.0'
        ]
    );

    private $client;
    private $responseFormat='JSON';//INT STRING HTML 
    /**
     * java老接口
     * @var
     */
    private $java_api;
    /**
     * java APP接口
     * @var
     */
    public $java_app;
    /**
     * java 新接口
     * @var
     */
    public $java_isz;
    /**
     * cms专用接口
     * @var
     */
    public $java_cms;

    public function __construct(){
        $config=$this->config;
        $this->client = new \GuzzleHttp\Client($config);
        $this->java_api = config('api.java_api');//rtrim(config('api.java_api'), '/');
        $this->java_app = config('api.java_app');//rtrim(config('api.java_app'), '/');
        $this->java_isz = config('api.java_isz');//rtrim(config('api.java_isz'), '/');
        $this->java_cms = config('api.java_cms');//rtrim(config('api.java_cms'), '/');
    }

    public function get($url,$data=array()){
        $config=$this->config;
        $config['query']=$data;
        $res = self::request('GET', $url,$config);
        return $res;
    }

    public function post($url,$data=array()){
        $config=$this->config;
        $config['form_params']=$data;
        $res = self::request('POST', $url,$config);
        return $res;
    }

    public function json($url,$data=array()){
        $config=$this->config;
        $config['json']=$data;
        $res = self::request('POST', $url, $config);
        return $res;
    }

    public function request($method,$url,$config){

        $resData=array('status'=>true,'code'=>200,'msg'=>'','obj'=>'');
        try{
            $res = $this->client->request($method, $url, $config);
            $httpStatusCode=$res->getStatusCode();
            self::setHttpStatusCode($httpStatusCode);
            if($httpStatusCode!==200){
                $resData['status']=false;
                $resData['code']=$httpStatusCode;
                $resData['msg']='服务器异常';
                return $resData;
            }
            $body=$res->getBody()->getContents();
            if(empty($body)){
                $resData['status']=false;
                $resData['msg']='返回为空';
                return $resData;
            }

            $ResponseFormat=self::getEnv('responseFormat');
            if($ResponseFormat==='JSON'){
                $array=json_decode($body);
                if(is_null($array)){
                    $resData['status']=false;
                    $resData['msg']='返回格式异常';
                    return $resData;
                }else{
                    $resData['obj']=(object)$array;
                }
            }else{
                $resData['obj']=$body;
            }
           return $resData;
        }catch(RequestException $e){
            $resData['status']=false;
            $resData['msg']='请求超时';
            $txt=$e->getHandlerContext();
            if($txt['errno']===28){ //超时
                $resData['msg']='请求超时';
            }
            
            if($e->hasResponse()){ 
               // echo  $e->getResponse(); 
            } 
            return $resData;
        }
        
       // $this->setHttpStatusCode($res->getStatusCode());
       // return $res;
    }

    public function sendAsy($method,$url,$data=array()){
        $config=$this->config;
        $this->setConfig('timeout',0.01);
        $res = self::$method( $url, $data);
    }

    public function verificationRes($res){
        if(is_object($res)){
            
        }
    }

    public function setHttpStatusCode($HttpStatusCode){
        $this->HttpStatusCode=$HttpStatusCode;
    }
    
    public function getHttpStatusCode(){
        return $this->HttpStatusCode;
    }

    public function setConfig($name='',$value=''){
        if($name==''){
            $this->config=$value;
        }
        $config=$this->config;
        $config[$name]=$value;
        $this->config=$config;
    }
    public function getConfig($value=''){
        $config=$this->config;
        if($value==''){
            return $config;
        }
        if(isset($config[$value])){
            return $config[$value];
        }
        return false;
    }

    public function setEnv($name,$value){
        if(isset($this->$name)){
            $this->$name=$value;
        }
    }

    public function getEnv($name){
        return $this->$name;
    }

    public function setMoblieRequest(){
        $config=$this->config;
        $config['headers']['User-Agent']=sprintf('%s %s',$config['headers']['User-Agent'],'iPhone');
        $this->config=$config;
    }


    
}